(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.lookup.calendarTypeController', calendarTypeController);
	calendarTypeController.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig',
			'authService', '$filter' ];
	function calendarTypeController($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, validatorConfig, authService, $filter) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("calendarTypeLookup");
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.ok = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](ctrl.selectedRows) !== false) {
				$uibModalInstance.close();
			}
		};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
		ctrl.init = function() {
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
			ctrl.gridOptions.data = 'vm.searchCriteria.list';
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			if(ctrl.dialogsModule.params.multiSelect == "true"){
				ctrl.gridOptions.multiSelect = true;
			}
			ctrl.calendarTypeOptions = calendarTypeOptions().data;
			ctrl.searchCriteria.filterCalendarTypes = [];
			if (ctrl.dialogsModule.params.filterCalendarTypes) {
				if(angular.isArray(ctrl.dialogsModule.params.filterCalendarTypes)){
					ctrl.searchCriteria.filterCalendarTypes = ctrl.dialogsModule.params.filterCalendarTypes;// get params from parent page
				}else if(angular.isString(ctrl.dialogsModule.params.filterCalendarTypes)){
					ctrl.searchCriteria.filterCalendarTypes.push(ctrl.dialogsModule.params.filterCalendarTypes);
				}
			} 
			ctrl.search();
		};

		function calendarTypeOptions() {
			return {
				"data" : [ {
					"label" : "label.calendartype.s",
					"value" : "S"
				},{
					"label" : "label.calendartype.y",
					"value" : "Y"
				},{
					"label" : "label.calendartype.t",
					"value" : "T"
				},{
					"label" : "label.calendartype.o",
					"value" : "O"
				},{
					"label" : "label.calendartype.c",
					"value" : "C"
				},{
					"label" : "label.calendartype.y",
					"value" : "Y"
				}]
			};
		}
	
		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			authService.search(ctrl.searchCriteria).then(function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			}, angular.noop);
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
	}
})(angular);